<?php
require("bases.php");

class ucenter extends bases {

    public function __construct(){
        parent::__construct();
        try{
            if(!$this->login_rs["id"])
                throw new Exception("请先登录，在进行该操作");
        }catch(Exception $e){
            echo json_encode(array("data"=>array(), "message"=>$e->getMessage(), "code"=>"0"));
            exit();
        }
    }

    /*
     * 获取个人中心首页信息
     * */
    public function index(){
        try {
            $redata = array();
            $postdata = $this->spArgs();
            $obj_student_info  = spClass("student_info");
            $student_info = $obj_student_info->join('sys_classes','sys_classes.id = sys_student_info.grade_id','left')->find(array("sys_student_info.uid" => $this->login_rs["id"]),null,'sys_student_info.*, sys_classes.name as grade_name');
            $redata = $student_info;
            $this->returnApi($redata);
        }catch(Exception $e){
            echo json_encode(array("data"=>"", "message"=>$e->getMessage(), "code"=>"0"));
            exit();
        }
    }


    public function modify(){
        try {
            $redata = array();
            $postdata = $this->spArgs();
            $obj_student = spClass("students");
            $data = array();
            $oldpass =  $postdata["oldPass"];
            $password =  $postdata["password"];
            $password2 =  $postdata["password2"];
            if(!$oldpass)
                throw new Exception("原始密码不能为空");
            if($this->login_rs["password"] != md5($oldpass))
                throw new Exception("原密码不正确");
            if(strlen($password) < 6)
                throw new Exception("密码必须不能少于6位");
            if($password != $password2)
                throw new Exception("两次密码不正确，请重新输入");
            if(!$obj_student->update(array("id"=>$this->login_rs["id"]), array("password"=>md5($password))))
                throw new Exception("未知错误，用户信息更新失败");
            $redata = array("message"=>"用户信息更新成功");
            $this->returnApi($redata, $this->login_rs);
        }catch(Exception $e){
            echo json_encode(array("data"=>"", "message"=>$e->getMessage(),  "code"=>"0"));
            exit();
        }
    }

    /**
     * 获取年级信息
     */
    public function getClasses(){
        try {
            $redata = array();
            $postdata = $this->spArgs();
            $obj_classes = spClass("classes");
            $data = array();
            $where = ' status = 1 and isdel = 0';
            if(!$redata['classes'] = $obj_classes->findAll($where))
                throw new Exception("找不到年级信息，请联系管理员！");
            $this->returnApi($redata);
        }catch(Exception $e){
            echo json_encode(array("data"=>"", "message"=>$e->getMessage(),  "code"=>"0"));
            exit();
        }
    }

    /**
     * 填写学生详情
     */
    public function info(){
        try{
            $redata = array();
            $postdata = $this ->spArgs();
            $obj_student_info = spClass('student_info');

            $info = $obj_student_info ->find(array('uid' =>$this->login_rs['id']));
            $data = array();
            $data['grade_id'] = intval($postdata['grade']);
            $data['classname'] = trim($postdata['classes']);
            $data['phone'] = $postdata['phone'];
            $data['name'] = $postdata['name'];
            if(!$data['grade_id'])
                throw new Exception("年级不能为空");
            if(!$data['classname'])
                throw new Exception("班级信息不能为空");
            if(!$data['phone'])
                throw new Exception("手机号不能为空");
            $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
            if (!preg_match($check, $data['phone']))
                throw new Exception("手机号不正确，请重新输入");
            if(!$data['name'])
                throw new Exception("您的姓名不能为空");
            if($info){
                if(!$obj_student_info->update(array("uid"=>$this->login_rs["id"]), $data))
                    throw new Exception("未知错误，用户信息更新失败");
            }else{
                $data['uid'] = $this->login_rs['id'];
                if(!$obj_student_info->create( $data))
                    throw new Exception("未知错误，用户信息更新失败");
            }
            $this->returnApi($redata,$this->login_rs);
        }catch(Exception $e){
            echo json_encode(array("data"=>"", "message"=>$e->getMessage(),  "code"=>"0"));
            exit();
        }
    }

    public function checkPass(){
        try{
            $redata = array();
            $postdata = $this->spArgs();
            $obj_student = spClass("students");
            if($obj_student ->checkPass(intval($postdata['uid']),$postdata['account'], $postdata['oldPass'])){
                $redata = array("message"=>"用户密码验证成功！");
                $this->returnApi($redata);
            }else{
                throw new \think\Exception('密码输入有误，请重新输入！');
            }

        }catch(Exception $e){
            echo json_encode(array("data"=>"", "message"=>$e->getMessage(), "code"=>"0"));
            exit();
        }
    }

}